#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#  Copyright (c) Huoty - All rights reserved
# 
#      Author: Huoty <sudohuoty@gmail.com>
#  CreateTime: 2016-11-08 11:43:58
# *************************************************************

import os.path
import time
 
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.gen
from tornado.ioloop import IOLoop
 
 
class Application(tornado.web.Application):
    def __init__(self):
        self.port = "8000"
 
        handlers = [
            (r"/", MainHandler),
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            debug=True,
        )
        tornado.web.Application.__init__(self, handlers, **settings)
 
 
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        list = ["a", "b", "c"]
        for i in list:
            tornado.ioloop.IOLoop.instance().add_callback(self.sleep, i)
            print("when i sleep")
 
    @tornado.gen.coroutine
    def sleep(self, i):
        time.sleep(5)
        print(i)

# Script starts from here

if __name__ == "__main__":
    application = Application()
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(application.port)
    tornado.ioloop.IOLoop.instance().start()
